package com.ogawa.project2213n.activity

import android.annotation.SuppressLint
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.ogawa.base.constants.Constants
 import com.ogawa.project2213N.base.Base2213NMqttActivity
import com.ogawa.project2213n.R
import com.ogawa.project2213n.bean.Massage2213NArmchair
import com.ogawa.projectcommon.constants.UrlPathConstant

@Route(path = UrlPathConstant.ACTIVITY_2213N_SEAT_ADJUST)
class Device2213nSeatAdjustActivity : Base2213NMqttActivity(), View.OnClickListener {
    private lateinit var tvCurrentPro: TextView
    private lateinit var tvRemainTime: TextView
    private lateinit var tvReset: TextView
    private lateinit var tvZero1: TextView
    private lateinit var tvZero2: TextView
    private lateinit var tvDaobei: TextView
    private lateinit var tvShenbei: TextView
    private lateinit var tvTaitui: TextView
    private lateinit var tvJiangtui: TextView
    override fun getLayoutId(): Int {
        return R.layout.activity_seat_adjust
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.seat_adjust))
        tvCurrentPro = findViewById(R.id.tv_current_program)
        tvRemainTime = findViewById(R.id.tv_remain_time)
        tvReset = findViewById(R.id.tv_reset)
        tvReset.setOnClickListener(this)
        tvZero1 = findViewById(R.id.tv_zero_1)
        tvZero1.setOnClickListener(this)
        tvZero2 = findViewById(R.id.tv_zero_2)
        tvZero2.setOnClickListener(this)
        tvDaobei = findViewById(R.id.tv_daobei)
        tvDaobei.setOnClickListener(this)
        tvShenbei = findViewById(R.id.tv_shenbei)
        tvShenbei.setOnClickListener(this)
        tvTaitui = findViewById(R.id.tv_taitui)
        tvTaitui.setOnClickListener(this)
        tvJiangtui = findViewById(R.id.tv_jiangtui)
        tvJiangtui.setOnClickListener(this)
//        MqttManager.getInstance().registerMqttDeviceStateListener(this)
//        MqttManager.getInstance().registerMqttSendMsgCallback(this)
    }

//    override fun onResume() {
//        super.onResume()
//        MqttManager.getInstance().setMqttConnectListener(this)
//    }
//
//    override fun onStop() {
//        super.onStop()
//        MqttManager.getInstance().clearMqttConnectListener()
//    }

//    override fun onDestroy() {
//        super.onDestroy()
//        MqttManager.getInstance().unRegisterMqttDeviceStateListener(this)
//        MqttManager.getInstance().unRegisterMqttSendMsgCallback(this)
//    }

    @SuppressLint("SetTextI18n")
    override fun onDeviceStateChange() {
        super.onDeviceStateChange()
        val deviceStatusBean = Massage2213NArmchair.getInstance().deviceStatusBean
        if (deviceStatusBean != null) {
            val autoProgram = deviceStatusBean.functions.autoProgram
            val autoValue = Integer.parseInt(autoProgram.modelValue)
            if (autoValue > 0) {
                tvCurrentPro.text = getString(R.string.current_program) + autoProgram.itemName
            }
            val healthProgram = deviceStatusBean.functions.lifeAutoProgram
            val healthValue = Integer.parseInt(healthProgram.modelValue)
            if (healthValue > 0) {
                tvCurrentPro.text = getString(R.string.current_program) + healthProgram.itemName
            }
            val advanceProgram = deviceStatusBean.functions.advanceAutoProgram
            val advanceValue = advanceProgram.modelValue
            if (advanceValue != "0") {
                tvCurrentPro.text =
                    getString(R.string.current_program) + getString(R.string.advance_massage)
            }
            if (autoValue == 0 && healthValue == 0 && advanceValue == "0") {//无程序运行
                tvCurrentPro.text = getString(R.string.current_program)
            }
            val min = deviceStatusBean.functions.timeMin.modelValue
            var sec = deviceStatusBean.functions.timeSec.modelValue
            if (sec.length < 2) {
                sec = "0$sec"
            }
            tvRemainTime.text = getString(R.string.remain_time) + min +getString(R.string.minute)
            val zeroGravity = deviceStatusBean.functions.zeroGravity
            val zeroValue = Integer.parseInt(zeroGravity.modelValue)
            tvReset.isSelected = zeroValue == 0
            tvZero1.isSelected = zeroValue == 1
            tvZero2.isSelected = zeroValue == 2
            val backPos = deviceStatusBean.functions.backPosAdjust
            val backPosValue = Integer.parseInt(backPos.modelValue)
            tvDaobei.isSelected = backPosValue == 0
            tvShenbei.isSelected = backPosValue == 1
            val legPos = deviceStatusBean.functions.legPosAdjust
            val legPosValue = Integer.parseInt(legPos.modelValue)
            tvJiangtui.isSelected = legPosValue == 0
            tvTaitui.isSelected = legPosValue == 1
        } /*else {
            finish()
        }*/
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_reset -> publish(Constants.ZERO_GRAVITY, "0")
            R.id.tv_zero_1 -> publish(Constants.ZERO_GRAVITY, "1")
            R.id.tv_zero_2 -> publish(Constants.ZERO_GRAVITY, "2")
            R.id.tv_daobei -> publish(Constants.BACK_POS_ADJUST, "0")
            R.id.tv_shenbei -> publish(Constants.BACK_POS_ADJUST, "1")
            R.id.tv_taitui -> publish(Constants.LEG_POS_ADJUST, "1")
            R.id.tv_jiangtui -> publish(Constants.LEG_POS_ADJUST, "0")
        }
    }
}